package com.me.components {
	import com.me.obj.Image;

	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.net.FileFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;

	import spark.components.Button;
	import spark.core.SpriteVisualElement;

	public class EventGrid extends SpriteVisualElement {
		public var event:TextField;
		public var button:TextField;
		public var dragArea:TextField;
		public var index:int = 0;
		public var img:Image;
		public var imgName:String;

		public function EventGrid(index:int) {
			this.index = index;
			graphics.clear();
			graphics.beginFill(0xFFCC66);
			graphics.drawRect(0, 0, 80, 80);
			graphics.endFill();
			createEvent();
			createButton();
			createDragArea();
		}

		private function createDragArea():void {
			dragArea = new TextField();
			dragArea.background = true;
			dragArea.backgroundColor = 0x00FF99;
			dragArea.type = TextFieldType.DYNAMIC;
			dragArea.autoSize = TextFieldAutoSize.CENTER;
			dragArea.selectable = false;
			dragArea.mouseEnabled = true;
			dragArea.text = "拖动区域";
			dragArea.alpha = .4;
			dragArea.x = (80 - dragArea.textWidth) >> 1;
			dragArea.y = (80 - dragArea.textHeight) >> 1;
			addChild(dragArea);
		}

		private function createEvent():void {
			event = new TextField();
//			event.background = true;
//			event.backgroundColor = 0xff0000;
			event.type = TextFieldType.INPUT;
			event.autoSize = TextFieldAutoSize.LEFT;
			event.text = "事件英文描述";
			addChild(event);
		}

		private function createButton():void {
			button = new TextField();
//			button.background = true;
//			button.backgroundColor = 0x993333;
			button.type = TextFieldType.DYNAMIC;
			button.autoSize = TextFieldAutoSize.LEFT;
			button.selectable = false;
			button.mouseEnabled = true;
			button.text = ["选择资源[Lc:", index, "]"].join("");
			button.y = 80 - button.textHeight - 2;
			button.addEventListener(MouseEvent.CLICK, clickHandler);
			addChild(button);
		}

		private function clickHandler(e:MouseEvent):void {
			var file:File = new flash.filesystem.File();
			file.addEventListener(Event.SELECT, function(e:Event):void {
				var selectedFile:File = e.currentTarget as File;
				if (img && contains(img)) {
					removeChild(img);
				}
				img = new Image(selectedFile.nativePath);
				var paths:Array = img.url.split("\\");
				imgName = paths[paths.length - 1];
				addChildAt(img, 0);
			});
			file.browseForOpen("请选择事件资源", [new FileFilter("资源(*.png,*.jpg,*.jpeg)", "*.png;*.jpg;*.jpeg")]);
		}
	}
}
